﻿namespace Util
{
    using System;
    using System.Collections.Generic;

    public static class Generic
    {
        public static void ShowListOnConsole<T>(IList<T> list)
        {
            if (list.Count > 0) Console.Write(list[0]);
            for (int i = 1; i < list.Count; ++i)
            {
                Console.Write(", " + list[i]);
            }
            Console.WriteLine();
        }

        public static int LowerBound<T>(IList<T> list, int start, int end, T value) where T : IComparable<T>
        {
            while (start < end)
            {
                int mid = (start + end) >> 1;
                if (list[mid].CompareTo(value) < 0)
                {
                    start = mid + 1;
                }
                else
                {
                    end = mid;
                }
            }
            return end;
        }

        public static int LowerBound<T>(IList<T> list, T value) where T : IComparable<T>
        {
            return LowerBound<T>(list, 0, list.Count, value);
        }

        public static int UpperBound<T>(IList<T> list, int start, int end, T value) where T : IComparable<T>
        {
            while (start < end)
            {
                int mid = (start + end) >> 1;
                if (value.CompareTo(list[mid]) < 0)
                {
                    end = mid;
                }
                else
                {
                    start = mid + 1;
                }
            }
            return end;
        }

        public static int UpperBound<T>(IList<T> list, T value) where T : IComparable<T>
        {
            return UpperBound<T>(list, 0, list.Count, value);
        }
    }
}
